package alipay.openplatform.edu.campus.jiuye.api.filter;

import alipay.openplatform.edu.campus.jiuye.common.exception.OnlineRuntimeException;
import alipay.openplatform.edu.campus.jiuye.service.dao.ApiResult;
import com.alibaba.fastjson.JSON;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.*;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * 总口控制
 *
 * @author zhangyang
 * @create 2016-08-30
 */
public class BaseFilter implements Filter {
    private final static Logger logger= LoggerFactory.getLogger(BaseFilter.class);
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
//        HttpServletRequest request= (HttpServletRequest) servletRequest;
        HttpServletResponse response= (HttpServletResponse) servletResponse;
        response.setContentType("text/html;charset=UTF-8");
        try {
            filterChain.doFilter(servletRequest, servletResponse);
        }catch (Exception e){
            logger.error("",e);
            PrintWriter pw=response.getWriter();
            pw.write(JSON.toJSONString(ApiResult.build("500",e.getMessage())));
            pw.flush();
            pw.close();
        }
    }

    public void destroy() {

    }
}
